package com.engine.kq.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.kq.service.KQDutyRulesService;
import com.engine.kq.service.KQDutyRulesService;
import com.engine.kq.service.impl.KQDutyRulesServiceImpl;
import com.engine.kq.service.impl.KQDutyRulesServiceImpl;
import weaver.conn.RecordSetTrans;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.workflow.form.FormManager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * 值班规则
 */

public class KQDutyRulesAction {

    public KQDutyRulesAction() {
    }

    private KQDutyRulesService getService(User user) {
        return (KQDutyRulesService) ServiceUtil.getService(KQDutyRulesServiceImpl.class, user);
    }



    /**
     * 获取查询列表
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getSearchList")
    @Produces(MediaType.TEXT_PLAIN)
    public String getSearchList(@Context HttpServletRequest request, @Context HttpServletResponse response) {

        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).getSearchList(ParamUtil.request2Map(request), user);
            apiDatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apiDatas.put("api_status", false);
            apiDatas.put("api_errormsg", e.getMessage());
        }
        return JSONObject.toJSONString(apiDatas);
    }

    /**
     * 获取新建或者编辑的表单
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getDutyRulesForm")
    @Produces(MediaType.TEXT_PLAIN)
    public String getDutyRulesForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).getDutyRulesForm(ParamUtil.request2Map(request), user);
            apiDatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apiDatas.put("api_status", false);
            apiDatas.put("api_errormsg", e.getMessage());
        }
        return JSONObject.toJSONString(apiDatas);
    }

    /**
     * 新建
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/addDutyRules")
    @Produces(MediaType.TEXT_PLAIN)
    public String addDutyRules(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).addDutyRules(ParamUtil.request2Map(request), user);
            apiDatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apiDatas.put("api_status", false);
            apiDatas.put("api_errormsg", e.getMessage());
        }
        return JSONObject.toJSONString(apiDatas);
    }

    /**
     * 编辑
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/editDutyRules")
    @Produces(MediaType.TEXT_PLAIN)
    public String editDutyRules(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).editDutyRules(ParamUtil.request2Map(request), user);
            apiDatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apiDatas.put("api_status", false);
            apiDatas.put("api_errormsg", e.getMessage());
        }
        return JSONObject.toJSONString(apiDatas);
    }

    /**
     * 删除
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/deleteDutyRules")
    @Produces(MediaType.TEXT_PLAIN)
    public String deleteDutyRules(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).deleteDutyRules(ParamUtil.request2Map(request), user);
            apiDatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apiDatas.put("api_status", false);
            apiDatas.put("api_errormsg", e.getMessage());
        }
        return JSONObject.toJSONString(apiDatas);
    }

//    /**
//     * 保存是否默认
//     *
//     * @param request
//     * @param response
//     * @return
//     */
//    @POST
//    @Path("/saveDefault")
//    @Produces(MediaType.TEXT_PLAIN)
//    public String saveDefault(@Context HttpServletRequest request, @Context HttpServletResponse response) {
//        Map<String, Object> apiDatas = new HashMap<String, Object>();
//        try {
//            User user = HrmUserVarify.getUser(request, response);
//            apiDatas = getService(user).saveDefault(ParamUtil.request2Map(request), user);
//            apiDatas.put("api_status", true);
//        } catch (Exception e) {
//            e.printStackTrace();
//            apiDatas.put("api_status", false);
//            apiDatas.put("api_errormsg", e.getMessage());
//        }
//        return JSONObject.toJSONString(apiDatas);
//    }

    /**
     * 判断某考勤组是否重复设置了加班规则
     *
     * @param request
     * @param response
     * @return
     */
//    @GET
//    @Path("/isRepeatSetting")
//    @Produces(MediaType.TEXT_PLAIN)
//    public String isRepeatSetting(@Context HttpServletRequest request, @Context HttpServletResponse response) {
//        Map<String, Object> apiDatas = new HashMap<String, Object>();
//        try {
//            User user = HrmUserVarify.getUser(request, response);
//            apiDatas = getService(user).isRepeatSetting(ParamUtil.request2Map(request), user);
//            apiDatas.put("api_status", true);
//        } catch (Exception e) {
//            e.printStackTrace();
//            apiDatas.put("api_status", false);
//            apiDatas.put("api_errormsg", e.getMessage());
//        }
//        return JSONObject.toJSONString(apiDatas);
//    }

    /**
     * 获取加班单位的表单
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getDutyUnitForm")
    @Produces(MediaType.TEXT_PLAIN)
    public String getDutyUnitForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).getDutyUnitForm(ParamUtil.request2Map(request), user);
            apiDatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apiDatas.put("api_status", false);
            apiDatas.put("api_errormsg", e.getMessage());
        }
        return JSONObject.toJSONString(apiDatas);
    }

    /**
     * 加班单位--保存
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/saveDutyUnit")
    @Produces(MediaType.TEXT_PLAIN)
    public String saveDutyUnit(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).saveDutyUnit(ParamUtil.request2Map(request), user);
            apiDatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apiDatas.put("api_status", false);
            apiDatas.put("api_errormsg", e.getMessage());
        }
        return JSONObject.toJSONString(apiDatas);
    }

}
